package com.example.administrator.yanfoxconn.activity;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;

import com.example.administrator.yanfoxconn.R;
import com.example.administrator.yanfoxconn.constant.Constants;
import com.example.administrator.yanfoxconn.utils.BaseActivity;
import com.github.barteksc.pdfviewer.PDFView;
import com.github.barteksc.pdfviewer.listener.OnLoadCompleteListener;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @Description 安保部 公文查詢 查看PDF文檔
 * @Author song
 * @Date 11/4/21 5:17 PM
 */
public class KLLookPDFActivity extends BaseActivity {
    PDFView pdfView;
    ProgressDialog dialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.look_pdf);
        String pdfName=getIntent().getStringExtra("pdf");
        pdfView = findViewById(R.id.pdfView);
        dialog = ProgressDialog.show(KLLookPDFActivity.this, "", "正在加载PDF", true);
        new RetrivePDFStream().execute(Constants.HTTP_KL_GET_DOWNLOAD+"?generateName="+pdfName);

    }



    class RetrivePDFStream extends AsyncTask<String, Void, InputStream> {

        protected InputStream doInBackground(String... strings) {
            InputStream inputStream = null;
            try {
                URL uri = new URL(strings[0]);
                HttpURLConnection urlConnection = (HttpURLConnection) uri.openConnection();
                if (urlConnection.getResponseCode() == 200) {
                    inputStream = new BufferedInputStream(urlConnection.getInputStream());
                }
            } catch (IOException e) {
                return null;
            }
            return inputStream;
        }

        protected void onPostExecute(InputStream inputStream) {
            pdfView.fromStream(inputStream)
                    .onLoad(new OnLoadCompleteListener() {
                        @Override
                        public void loadComplete(int nbPages) {
                            dialog.dismiss();
                        }
                    })
                    .load();
        }
    }
}
